﻿Imports System.Windows.Input
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Controls

Public Class EditingGrid

    Friend Shared Associations As New Dictionary(Of Object, Grid)

    'Sub New()

    '    ' Cet appel est requis par le concepteur.
    '    InitializeComponent()

    '    '' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
    '    'zt.Visibility = Windows.Visibility.Collapsed
    '    'zb.Visibility = Windows.Visibility.Collapsed
    '    'zl.Visibility = Windows.Visibility.Collapsed
    '    'zr.Visibility = Windows.Visibility.Collapsed
    'End Sub


    ''Private Sub EditindGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyDown
    ''    If e.Key = Key.LeftShift Or e.Key = Key.RightShift Then
    ''        zt.Visibility = Windows.Visibility.Visible
    ''        zb.Visibility = Windows.Visibility.Visible
    ''        zl.Visibility = Windows.Visibility.Visible
    ''        zr.Visibility = Windows.Visibility.Visible
    ''    End If
    ''End Sub

    ''Private Sub EditindGrid_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
    ''    If e.Key = Key.LeftShift Or e.Key = Key.RightShift Then
    ''        zt.Visibility = Windows.Visibility.Collapsed
    ''        zb.Visibility = Windows.Visibility.Collapsed
    ''        zl.Visibility = Windows.Visibility.Collapsed
    ''        zr.Visibility = Windows.Visibility.Collapsed
    ''    End If
    ''End Sub


    'Private Sub EditindGrid_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseMove
    '    If Keyboard.Modifiers = ModifierKeys.Shift Then
    '        zt.Visibility = Windows.Visibility.Visible
    '        zb.Visibility = Windows.Visibility.Visible
    '        zl.Visibility = Windows.Visibility.Visible
    '        zr.Visibility = Windows.Visibility.Visible
    '        hthumb.Visibility = Windows.Visibility.Visible

    '    Else
    '        zt.Visibility = Windows.Visibility.Collapsed
    '        zb.Visibility = Windows.Visibility.Collapsed
    '        zl.Visibility = Windows.Visibility.Collapsed
    '        zr.Visibility = Windows.Visibility.Collapsed
    '        hthumb.Visibility = Windows.Visibility.Collapsed
    '    End If
    'End Sub

    'Private Sub Rect_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
    '    If sender Is zl Or sender Is zr Then
    '        Dim pt = e.GetPosition(grd)

    '        hthumb.Margin = New Windows.Thickness(0.0, pt.Y, 0.0, 0.0)
    '    End If
    'End Sub

    Private Sub Zameul()

        Dim o = <Grid Name="grd">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" MinWidth="1"/>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto" MinWidth="1"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" MinHeight="1"/>
                        <RowDefinition/>
                        <RowDefinition Height="Auto" MinHeight="1"/>
                    </Grid.RowDefinitions>

                    <Rectangle Name="zt" Grid.ColumnSpan="3" Height="10" Fill="Black" MouseMove="Rect_MouseMove" Cursor="Cross"/>
                    <Rectangle Name="zl" Grid.RowSpan="3" Fill="Black" Width="10" MouseMove="Rect_MouseMove" Cursor="Cross"/>
                    <Rectangle Name="zb" Grid.ColumnSpan="3" Grid.Row="2" Height="10" Fill="Black" MouseMove="Rect_MouseMove" Cursor="Cross"/>
                    <Rectangle Name="zr" Grid.RowSpan="3" Grid.Column="2" Fill="Black" Width="10" MouseMove="Rect_MouseMove" Cursor="Cross"/>

                    <Rectangle Name="hthumb" Fill="Red" Stroke="Red" StrokeThickness="2" Grid.ColumnSpan="3" Grid.RowSpan="3" Height="5" VerticalAlignment="Top" HorizontalAlignment="Stretch" StrokeDashCap="Triangle" Stretch="Fill" Cursor="Cross"/>



                </Grid>
    End Sub

    Public Enum Definition
        Auto
        Percentage
        Absolute
    End Enum

    Public Class GridDefinition

        Dim brotherrow As Controls.RowDefinition
        Dim brothercolumn As Controls.ColumnDefinition

        Public Property Size As Double
        Public Property Mode As Definition

        Sub New()

        End Sub

        Sub New(ByVal brotherrow As Controls.RowDefinition)
            Me.brotherrow = brotherrow
        End Sub
        Sub New(ByVal brothercolumn As Controls.ColumnDefinition)
            Me.brothercolumn = brothercolumn
        End Sub


    End Class

    Public Property Rows As IEnumerable(Of GridDefinition)
        Get
            Return GetValue(RowsProperty)
        End Get

        Set(ByVal value As IEnumerable(Of GridDefinition))
            SetValue(RowsProperty, value)
        End Set
    End Property

    Public Shared ReadOnly RowsProperty As DependencyProperty = _
                           DependencyProperty.Register("Rows", _
                           GetType(IEnumerable(Of GridDefinition)), GetType(EditingGrid), _
                           New FrameworkPropertyMetadata(Nothing))

    Public Property Columns As IEnumerable(Of GridDefinition)
        Get
            Return GetValue(ColumnsProperty)
        End Get

        Set(ByVal value As IEnumerable(Of GridDefinition))
            SetValue(ColumnsProperty, value)
        End Set
    End Property

    Public Shared ReadOnly ColumnsProperty As DependencyProperty = _
                           DependencyProperty.Register("Columns", _
                           GetType(IEnumerable(Of GridDefinition)), GetType(EditingGrid), _
                           New FrameworkPropertyMetadata(Nothing))


    Public Property SplitterSize As Double
        Get
            Return GetValue(SplitterSizeProperty)
        End Get

        Set(ByVal value As Double)
            SetValue(SplitterSizeProperty, value)
        End Set
    End Property

    Public Shared ReadOnly SplitterSizeProperty As DependencyProperty = _
                           DependencyProperty.Register("SplitterSize", _
                           GetType(Double), GetType(EditingGrid), _
                           New FrameworkPropertyMetadata(2.0))

    Private locked As Boolean? = Nothing

    Private Sub Setup(ByVal RowCount As Integer, ByVal ColumnCount As Integer)
        If Associations.ContainsKey(DataContext) Then
            root.Child = Associations(DataContext)
            Return
        End If

        Dim uiser = ModuleIndicateurs.AppInstance.GetService(Of Common.UI.IUIService)()


        Dim grd As New Controls.Grid()

        If RowCount > 1 Then
            Dim qr = From elt In Enumerable.Range(0, 2 * RowCount - 1)
                     Select New Controls.RowDefinition() With { _
                         .Height = If(elt Mod 2 = 1, GridLength.Auto, New GridLength(1.0, GridUnitType.Star))}

            For Each elt In qr
                'If elt.Height.IsAbsolute Then

                '    Dim b = New Binding("SplitterSize")
                '    b.Source = Me
                '    b.Mode = BindingMode.OneWay
                '    b.Converter = DoubleGridLengthConverter.Instance

                '    BindingOperations.SetBinding(elt, Controls.RowDefinition.HeightProperty, b)
                'End If
                grd.RowDefinitions.Add(elt)
            Next


        End If

        If ColumnCount > 1 Then
            Dim qr = From elt In Enumerable.Range(0, 2 * ColumnCount - 1)
                     Select New Controls.ColumnDefinition() With { _
                         .Width = If(elt Mod 2 = 1, GridLength.Auto, New GridLength(1.0, GridUnitType.Star))}

            For Each elt In qr
                'If elt.Width.IsAbsolute Then

                '    Dim b = New Binding("SplitterSize")
                '    b.Source = Me
                '    b.Mode = BindingMode.OneWay
                '    b.Converter = DoubleGridLengthConverter.Instance

                '    BindingOperations.SetBinding(elt, Controls.ColumnDefinition.WidthProperty, b)
                'End If
                grd.ColumnDefinitions.Add(elt)
            Next
        End If

        If RowCount > 1 Then
            For i As Integer = 1 To 2 * RowCount - 2 Step 2
                Dim sp As New Controls.GridSplitter()
                '     sp.Style = Me.Resources("hsplitter")

                sp.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
                sp.VerticalAlignment = Windows.VerticalAlignment.Stretch
                sp.Background = Windows.Media.Brushes.Violet 'Windows.Media.Brushes.CornflowerBlue
                sp.Opacity = 0.5
                sp.Height = 2.0

                AddHandler sp.MouseEnter, Sub() sp.Height = 10.0
                AddHandler sp.MouseLeave, Sub() sp.Height = 2.0
                AddHandler sp.MouseDoubleClick, _
                    Sub() _
                        uiser.ShowMessage( _
                            New Views.ViewGridDefinitionEdtior( _
                                grd.RowDefinitions(Grid.GetRow(sp) - 1), _
                                grd.RowDefinitions(Grid.GetRow(sp) + 1)))

                grd.Children.Add(sp)

                Grid.SetColumnSpan(sp, 2 * ColumnCount - 1)
                Grid.SetRowSpan(sp, 1)
                Grid.SetColumn(sp, 0)
                Grid.SetRow(sp, i)


            Next
        End If

        If ColumnCount > 1 Then
            For i As Integer = 1 To 2 * ColumnCount - 2 Step 2
                Dim sp As New Controls.GridSplitter()
                sp.HorizontalAlignment = Windows.HorizontalAlignment.Stretch
                sp.VerticalAlignment = Windows.VerticalAlignment.Stretch
                sp.Background = Windows.Media.Brushes.Violet 'Windows.Media.Brushes.CornflowerBlue
                sp.Opacity = 0.5
                sp.Width = 2.0

                AddHandler sp.MouseEnter, Sub() sp.Width = 10.0
                AddHandler sp.MouseLeave, Sub() sp.Width = 2.0
                AddHandler sp.MouseDoubleClick, Sub() uiser.ShowMessage(New Views.ViewGridDefinitionEdtior(grd.ColumnDefinitions(Grid.GetColumn(sp) - 1)))

                grd.Children.Add(sp)

                Grid.SetRowSpan(sp, 2 * RowCount - 1)
                Grid.SetColumnSpan(sp, 1)
                Grid.SetRow(sp, 0)
                Grid.SetColumn(sp, i)
            Next
        End If

        root.Child = grd
   
        Associations.Add(DataContext, grd)

        Dim o = TryCast(DataContext, VMDashboardDefinition)

        AddHandler o.Zones.CollectionChanged, Sub(sobject As Object, evtarg As Specialized.NotifyCollectionChangedEventArgs)
                                                  ZonesCollectionChanged(grd, sobject, evtarg)
                                              End Sub


        locked = True

        If o.Zones.Count > 0 Then
            For Each z In o.Zones
                'Dim tg = New EditZone()
                'Grid.SetColumnSpan(tg, 2 * z.ColumnCount - 1)
                'Grid.SetRow(tg, 2 * z.StartRow)
                'Grid.SetColumn(tg, 2 * z.StartColumn)
                'Grid.SetRowSpan(tg, 2 * z.RowCount - 1)
                'tg.DataContext = z
                grd.Children.Add(z.prebuildeditzone)
            Next
        End If
    End Sub

    Private Sub EditindGrid_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
        root.Child = Nothing

        Dim o = TryCast(e.NewValue, VMDashboardDefinition)
        'Dim old = TryCast(e.OldValue, VMDashboardDefinition)

        'If old IsNot Nothing Then
        '    RemoveHandler old.Zones.CollectionChanged, AddressOf ZonesCollectionChanged
        'End If
        If o Is Nothing Then Return

        Setup(o.LayoutDefinition.RowCount, o.LayoutDefinition.ColumnCount)

    End Sub

    Private Sub ZonesCollectionChanged(ByVal owner As Grid, ByVal sender As Object, ByVal e As Specialized.NotifyCollectionChangedEventArgs)
        Select Case e.Action
            Case Specialized.NotifyCollectionChangedAction.Add
                Dim z As VMDashboardDefinitionZone = e.NewItems(0)
                Dim tg = New EditZone()
                Grid.SetColumnSpan(tg, 2 * z.ColumnCount - 1)
                Grid.SetRow(tg, 2 * z.StartRow)
                Grid.SetColumn(tg, 2 * z.StartColumn)
                Grid.SetRowSpan(tg, 2 * z.RowCount - 1)
                tg.DataContext = z

                'Setup(tg, z)
                'If DataContext Is Nothing Then
                '    Dim o = 1
                'Else
                '    Associations(DataContext).Children.Add(tg)
                'End If
                owner.Children.Add(tg)

            Case Specialized.NotifyCollectionChangedAction.Remove
                Dim q = (From elt As Control In Associations(DataContext).Children
                        Where elt.DataContext Is e.OldItems(0)
                        Select elt).FirstOrDefault()

                If q IsNot Nothing Then
                    Associations(DataContext).Children.Remove(q)
                End If
        End Select
    End Sub

    Private Sub Setup(ByVal tg As EditZone, ByVal z As VMDashboardDefinitionZone)
        Dim rect As New Border()
        rect.Background = Media.Brushes.Black
        rect.Width = 5
        rect.HorizontalAlignment = Windows.HorizontalAlignment.Left

        Dim b = New Binding("(Grid.Row)")
        b.Source = tg
        BindingOperations.SetBinding(rect, Grid.RowProperty, b)


        Associations(DataContext).Children.Add(rect)

    End Sub

   


End Class

Public Class DoubleGridLengthConverter
    Implements IValueConverter

    Friend Shared Instance As DoubleGridLengthConverter = New DoubleGridLengthConverter()

    Private Sub New()

    End Sub

    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        Return New GridLength(CDbl(value))
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

Public Class EditindGridCommand

End Class

Public Class PositionRelativeToAbsoluteConverter
    Implements IValueConverter


    Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

Public Class ZoneRule
    Inherits Control



    Public Property LineStyle As Style
        Get
            Return GetValue(LineStyleProperty)
        End Get

        Set(ByVal value As Style)
            SetValue(LineStyleProperty, value)
        End Set
    End Property

    Public Shared ReadOnly LineStyleProperty As DependencyProperty = _
                           DependencyProperty.Register("LineStyle", _
                           GetType(Style), GetType(ZoneRule), _
                           New FrameworkPropertyMetadata(Nothing))



End Class